package hi.chyl.json;

import java.awt.AWTEvent;
import java.awt.EventQueue;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.FlavorEvent;
import java.awt.datatransfer.FlavorListener;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.Caret;
import javax.swing.text.JTextComponent;
import org.jdesktop.application.AbstractBean;
import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationActionMap;
import org.jdesktop.application.ApplicationContext;

class TextActions extends AbstractBean
{

    private final ApplicationContext context;
    private final CaretListener textComponentCaretListener;
    private final PropertyChangeListener textComponentPCL;
    private final String markerActionKey = "TextActions.markerAction";
    private final javax.swing.Action markerAction;
    private boolean copyEnabled = false;
    private boolean cutEnabled = false;
    private boolean pasteEnabled = false;
    private boolean deleteEnabled = false;

    public TextActions(ApplicationContext paramApplicationContext)
    {
        this.context = paramApplicationContext;
        this.markerAction = new AbstractAction()
        {
            public void actionPerformed(ActionEvent paramAnonymousActionEvent)
            {
            }
        };
        this.textComponentCaretListener = new TextComponentCaretListener();
        this.textComponentPCL = new TextComponentPCL();
        getClipboard().addFlavorListener(new ClipboardListener());
    }

    private ApplicationContext getContext()
    {
        return this.context;
    }

    private JComponent getFocusOwner()
    {
        return getContext().getFocusOwner();
    }

    private Clipboard getClipboard()
    {
        return getContext().getClipboard();
    }

    void updateFocusOwner(JComponent paramJComponent1, JComponent paramJComponent2)
    {
        JTextComponent localJTextComponent;
        if ((paramJComponent1 instanceof JTextComponent))
        {
            localJTextComponent = (JTextComponent) paramJComponent1;
            localJTextComponent.removeCaretListener(this.textComponentCaretListener);
            localJTextComponent.removePropertyChangeListener(this.textComponentPCL);
        }
        if ((paramJComponent2 instanceof JTextComponent))
        {
            localJTextComponent = (JTextComponent) paramJComponent2;
            maybeInstallTextActions(localJTextComponent);
            updateTextActions(localJTextComponent);
            localJTextComponent.addCaretListener(this.textComponentCaretListener);
            localJTextComponent.addPropertyChangeListener(this.textComponentPCL);
        } else if (paramJComponent2 == null)
        {
            setCopyEnabled(false);
            setCutEnabled(false);
            setPasteEnabled(false);
            setDeleteEnabled(false);
        }
    }

    private void updateTextActions(JTextComponent paramJTextComponent)
    {
        Caret localCaret = paramJTextComponent.getCaret();
        boolean bool1 = localCaret.getDot() != localCaret.getMark();
        boolean bool2 = paramJTextComponent.isEditable();
        boolean bool3 = getClipboard().isDataFlavorAvailable(DataFlavor.stringFlavor);
        setCopyEnabled(bool1);
        setCutEnabled((bool2) && (bool1));
        setDeleteEnabled((bool2) && (bool1));
        setPasteEnabled((bool2) && (bool3));
    }

    private void maybeInstallTextActions(JTextComponent paramJTextComponent)
    {
        ActionMap localActionMap = paramJTextComponent.getActionMap();
        if (localActionMap.get("TextActions.markerAction") == null)
        {
            localActionMap.put("TextActions.markerAction", this.markerAction);
            ApplicationActionMap localApplicationActionMap = getContext().getActionMap(getClass(), this);
            for (Object localObject : localApplicationActionMap.keys())
            {
                localActionMap.put(localObject, localApplicationActionMap.get(localObject));
            }
        }
    }

    private int getCurrentEventModifiers()
    {
        int i = 0;
        AWTEvent localAWTEvent = EventQueue.getCurrentEvent();
        if ((localAWTEvent instanceof InputEvent))
        {
            i = ((InputEvent) localAWTEvent).getModifiers();
        } else if ((localAWTEvent instanceof ActionEvent))
        {
            i = ((ActionEvent) localAWTEvent).getModifiers();
        }
        return i;
    }

    private void invokeTextAction(JTextComponent paramJTextComponent, String paramString)
    {
        ActionMap localActionMap = paramJTextComponent.getActionMap().getParent();
        long l = EventQueue.getMostRecentEventTime();
        int i = getCurrentEventModifiers();
        ActionEvent localActionEvent = new ActionEvent(paramJTextComponent, 1001, paramString, l, i);

        localActionMap.get(paramString).actionPerformed(localActionEvent);
    }

    @Action(enabledProperty = "cutEnabled")
    public void cut(ActionEvent paramActionEvent)
    {
        Object localObject = paramActionEvent.getSource();
        if ((localObject instanceof JTextComponent))
        {
            invokeTextAction((JTextComponent) localObject, "cut");
        }
    }

    public boolean isCutEnabled()
    {
        return this.cutEnabled;
    }

    public void setCutEnabled(boolean paramBoolean)
    {
        boolean bool = this.cutEnabled;
        this.cutEnabled = paramBoolean;
        firePropertyChange("cutEnabled", Boolean.valueOf(bool), Boolean.valueOf(this.cutEnabled));
    }

    @Action(enabledProperty = "copyEnabled")
    public void copy(ActionEvent paramActionEvent)
    {
        Object localObject = paramActionEvent.getSource();
        if ((localObject instanceof JTextComponent))
        {
            invokeTextAction((JTextComponent) localObject, "copy");
        }
    }

    public boolean isCopyEnabled()
    {
        return this.copyEnabled;
    }

    public void setCopyEnabled(boolean paramBoolean)
    {
        boolean bool = this.copyEnabled;
        this.copyEnabled = paramBoolean;
        firePropertyChange("copyEnabled", Boolean.valueOf(bool), Boolean.valueOf(this.copyEnabled));
    }

    @Action(enabledProperty = "pasteEnabled")
    public void paste(ActionEvent paramActionEvent)
    {
        Object localObject = paramActionEvent.getSource();
        if ((localObject instanceof JTextComponent))
        {
            invokeTextAction((JTextComponent) localObject, "paste");
        }
    }

    public boolean isPasteEnabled()
    {
        return this.pasteEnabled;
    }

    public void setPasteEnabled(boolean paramBoolean)
    {
        boolean bool = this.pasteEnabled;
        this.pasteEnabled = paramBoolean;
        firePropertyChange("pasteEnabled", Boolean.valueOf(bool), Boolean.valueOf(this.pasteEnabled));
    }

    @Action(enabledProperty = "deleteEnabled")
    public void delete(ActionEvent paramActionEvent)
    {
        Object localObject = paramActionEvent.getSource();
        if ((localObject instanceof JTextComponent))
        {
            invokeTextAction((JTextComponent) localObject, "delete-next");
        }
    }

    public boolean isDeleteEnabled()
    {
        return this.deleteEnabled;
    }

    public void setDeleteEnabled(boolean paramBoolean)
    {
        boolean bool = this.deleteEnabled;
        this.deleteEnabled = paramBoolean;
        firePropertyChange("deleteEnabled", Boolean.valueOf(bool), Boolean.valueOf(this.deleteEnabled));
    }

    private final class ClipboardListener
            implements FlavorListener
    {

        private ClipboardListener()
        {
        }

        public void flavorsChanged(FlavorEvent paramFlavorEvent)
        {
            JComponent localJComponent = TextActions.this.getFocusOwner();
            if ((localJComponent instanceof JTextComponent))
            {
                TextActions.this.updateTextActions((JTextComponent) localJComponent);
            }
        }
    }

    private final class TextComponentCaretListener implements CaretListener
    {

        private TextComponentCaretListener()
        {
        }

        public void caretUpdate(CaretEvent paramCaretEvent)
        {
            TextActions.this.updateTextActions((JTextComponent) paramCaretEvent.getSource());
        }
    }

    private final class TextComponentPCL implements PropertyChangeListener
    {

        private TextComponentPCL()
        {
        }

        public void propertyChange(PropertyChangeEvent paramPropertyChangeEvent)
        {
            String str = paramPropertyChangeEvent.getPropertyName();
            if ((str == null) || ("editable".equals(str)))
            {
                TextActions.this.updateTextActions((JTextComponent) paramPropertyChangeEvent.getSource());
            }
        }
    }
    
    public static JPopupMenu createTextPopMenu() {
        ApplicationActionMap actionMap = Application.getInstance().getContext().getActionMap();
        JPopupMenu popMenu = new JPopupMenu();

        JMenuItem mtCopy = new JMenuItem(actionMap.get("copy"));
        JMenuItem mtPaste = new JMenuItem(actionMap.get("paste"));
        JMenuItem mtCut = new JMenuItem(actionMap.get("cut"));
        JMenuItem mtClean = new JMenuItem(actionMap.get("delete"));

        popMenu.add(mtCut);
        popMenu.add(mtCopy);
        popMenu.add(mtPaste);
        popMenu.add(mtClean);

        return popMenu;
    }
}